//
//  DHBindingPhoneVC.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/12.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHBindingPhoneVC.h"
#import "DHLoginInfoViewController.h"
#import "DHLoginStyleViewController.h"

@interface DHBindingPhoneVC ()

@property (weak, nonatomic) IBOutlet UIButton *ensureButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet UIButton *codeButton;

@end

@implementation DHBindingPhoneVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.backButton.hidden = YES;
    
    self.titleLabel.text = @"绑定手机号码";
    
    
    [self.codeButton bk_addEventHandler:^(id sender) {
        if ([self.phoneTF.text isValidPhone]) {
            [[DHNetAPIClient shareDHNetAPIClient] request_smscodeWithPhone:self.phoneTF.text AndBlock:^(id data, NSError *error) {
                __block int leftTime = 60;
                self.codeButton.backgroundColor = [UIColor colorWithHexString:@"#cccccc"];
                [self.codeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [self.codeButton setTitle:[NSString stringWithFormat:@"已发送(%is)", leftTime] forState:UIControlStateDisabled];
                self.codeButton.layer.borderColor = [UIColor colorWithHexString:@"#cccccc"].CGColor;
                self.codeButton.enabled = NO;
                [NSTimer bk_scheduledTimerWithTimeInterval:1 block:^(NSTimer *timer) {
                    if (leftTime == 0) {
                        self.codeButton.enabled = YES;
                        [self.codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
                        [self.codeButton setTitleColor:[UIColor colorWithHexString:@"#4a4a4a"] forState:UIControlStateNormal];
                        self.codeButton.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
                        //计时完成，销毁定时器
                        [timer invalidate];
                    }
                    leftTime--;
                    [sender setTitle:[NSString stringWithFormat:@"已发送(%is)", leftTime] forState:UIControlStateDisabled];
                } repeats:YES];
            }];
        }else{
            [NSObject showHudTipStr:@"您输入的手机号码格式有错误"];
        }
    } forControlEvents:UIControlEventTouchUpInside];
    
    [self.ensureButton bk_addEventHandler:^(id sender) {
        if (![self.phoneTF.text isValidPhone]) {
            [NSObject showHudTipStr:@"您输入的手机号码格式有错误"];
            return ;
        }
        if (self.codeTF.text.length == 0) {
            [NSObject showHudTipStr:@"请输入验证码"];
            return;
        }
        [[DHNetAPIClient shareDHNetAPIClient] request_supplement_phoneWithParams:@{@"phone":self.phoneTF.text,@"smscode":self.codeTF.text} andBlock:^(id data, NSError *error) {
            if (data) {
                [Login curLoginUser].phone = self.phoneTF.text;
                [Login doLogin:[Login curLoginUser]];
                [[GlobalManager shareGlobalManager] refreshBasicInfoAndBlock:^(id data, NSError *error) {
                    [kAppDelegate configHomeController];
                }];
            }
        }];
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
